package biz.bbtec.sbm.store.service;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import biz.bbtec.sbm.db.dbConnection;
import biz.bbtec.sbm.store.bean.AddressBean;
import biz.bbtec.sbm.user.bean.UserBean;

public class AddressService {

	dbConnection db = new dbConnection();

	public boolean AddAddress(AddressBean add) {
		boolean flag = false;

		String sql1 = "insert into address(name,description,orgid) values ("
				+ "'" + add.getName() + "'" + "," + "'" + add.getDescription()
				+ "'" + "," + "'" + add.getOrgid() + "'" + ")";

		Connection conn = db.getConnection();
		PreparedStatement past = null;
		try {
			past = conn.prepareStatement(sql1);
			past.executeUpdate(sql1);
			flag = true;
			past.close();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			db.closeConnection(conn);
		}
		return flag;
	}
	
	public List showalladdress(UserBean user){
		List<AddressBean> list = new ArrayList<AddressBean>();
		
		String sql1 = "select * from address where orgid = ?";
		
		Connection conn = db.getConnection();
		PreparedStatement past = null;
		ResultSet rs = null;
		
		try{
			past = conn.prepareStatement(sql1);
			past.setInt(1, user.getOrgid());
			rs = past.executeQuery();
			while(rs.next()){
				AddressBean add = new AddressBean();
				add.setAdsid(rs.getInt("adsid"));
				add.setDescription(rs.getString("description"));
				add.setName(rs.getString("name"));
				add.setOrgid(user.getOrgid());
				list.add(add);
			}
			rs.close();
			past.close();
		}catch(SQLException e){
			e.printStackTrace();
		}finally{
			db.closeConnection(conn);
		}
		return list;
	}
}
